package com.prs.crm.action.oa;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.oa.RegulationType;
import com.prs.crm.domain.oa.SynodType;
import com.prs.crm.service.oa.SynodTypeService;
import com.prs.crm.service.sys.LogService;

public class SynodTypeAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private SynodType synodType;
	private SynodTypeService synodTypeService;
	private PaginationSupport pagination;
	private Integer[] synodTypeIds;
	private Collection<TreeNode> nodes;
	private LogService logService;

	@Action("listSynodType")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				SynodType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getSynodTypeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("selectSynodType")
	public String selectSynodType() {
		this.execute();
		return SUCCESS;
	}

	@Action(value = "loadSynodTypeTree", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadSynodTypeTree() {
		nodes = new ArrayList<TreeNode>();
		List<SynodType> regs = this.getSynodTypeService().getAllSynodType();
		for (SynodType reg : regs) {
			TreeNode node = new TreeNode();
			node.setId(reg.getId().toString());
			node.setText(reg.getName());
			nodes.add(node);
		}
		return SUCCESS;
	}

	@Action("editSynodType")
	public String edit() {
		return SUCCESS;
	}

	@Action(value = "saveSynodType", results = {
			@Result(name = "success", type = "redirect", location = "listSynodType"),
			@Result(name = "error", type = "chain", location = "editSynodType") })
	public String save() {
		if (synodType.getName() == null
				|| "".equals(synodType.getName().trim())) {
			addActionError("类型名称不能为空");
			return ERROR;
		}
		if (this.getLogService().getSearchName(SynodType.class,
				synodType.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getSynodTypeService().save(synodType);
		return SUCCESS;
	}

	@Action(value = "removeSynodType", results = {
			@Result(name = "success", type = "redirect", location = "listSynodType"),
			@Result(name = "error", type = "chain", location = "listSynodType") })
	public String remove() {
		if (synodTypeIds == null) {
			this.addActionError("请选择要删除的会议类型");
			return ERROR;
		}
		for (Integer id : synodTypeIds) {
			SynodType type = this.getSynodTypeService().get(id);
			if (type.getSynods().size() != 0) {
				this.addActionError("已有会议引用，不能删除");
				return ERROR;
			}
		}
		this.getSynodTypeService().batchRemove(synodTypeIds);
		return SUCCESS;
	}

	public SynodType getSynodType() {
		return synodType;
	}

	public void setSynodType(SynodType synodType) {
		this.synodType = synodType;
	}

	public SynodTypeService getSynodTypeService() {
		return synodTypeService;
	}

	public void setSynodTypeService(SynodTypeService synodTypeService) {
		this.synodTypeService = synodTypeService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getSynodTypeIds() {
		return synodTypeIds;
	}

	public void setSynodTypeIds(Integer[] synodTypeIds) {
		this.synodTypeIds = synodTypeIds;
	}

	public Collection<TreeNode> getNodes() {
		return nodes;
	}

	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
